package welsch.androidwerwolf.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import welsch.androidwerwolf.Globals;
import welsch.androidwerwolf.Player;
import welsch.androidwerwolf.R;
import welsch.androidwerwolf.adapter.PlayerListAdapter;

public class PlayerSettings extends ActionBarActivity  {

    ListView cListViewPlayer = null;
    PlayerListAdapter cAdapter = null;
    ImageButton cAddPlayerButton = null;
    EditText cEditTextPlayerName = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_settings);

        cListViewPlayer = (ListView) findViewById(R.id.ListViewPlayerSettingsActivity);
        cAddPlayerButton = (ImageButton) findViewById(R.id.cBtnAddNewPlayer);
        cEditTextPlayerName = (EditText) findViewById(R.id.editTextPlayerName);

        cAdapter  =new PlayerListAdapter(PlayerSettings.this, R.layout.player_list_item, Globals.gPlayerList);
        cListViewPlayer.setAdapter(cAdapter);
        initAddButton();

    }

    private void initAddButton(){

        cAddPlayerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String playerName = cEditTextPlayerName.getText().toString();
                Player newPlayer = new Player(playerName);
                cAdapter.add(newPlayer);
                cAdapter.notifyDataSetChanged();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_player_settings, menu);
        return true;
    }

    public void textPlayerNameChanged(View v) {
        Player player = (Player)v.getTag();
        String name= ((EditText)v).getText().toString();
        player.setName(name);
    }

    public void removePlayerOnClickHandler(View v) {
        Player itemToRemove = (Player)v.getTag();
        cAdapter.remove(itemToRemove);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
